package com.starblink.preferenceconfig.brandchoice.ui

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.blankj.utilcode.util.GsonUtils
import com.kunminx.architecture.domain.message.MutableResult
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.bridge.BridgeUtil
import com.starblink.android.basic.config.ColdLaunchConfig
import com.starblink.android.basic.ext.toast
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.android.basic.util.bus.bean.FollowStoreEvent
import com.starblink.android.basic.widget.LoadingDialogManager
import com.starblink.basic.log.SKLogger
import com.starblink.basic.storage.SkStorage
import com.starblink.basic.util.ActivityStackManage
import com.starblink.preferenceconfig.bean.PreferenceBrand
import com.starblink.rocketreserver.ListPreferenceConfigABTestQuery
import com.starblink.rocketreserver.ListSimilarPreferenceBrandConfigQuery
import com.starblink.rocketreserver.ReportPreferenceConfigMutation
import com.starblink.rocketreserver.type.PreferenceConfigRequest
import kotlinx.coroutines.launch

class BrandChoiceVM : BaseViewModel() {
    val totalPreferenceList = mutableListOf<PreferenceBrand>() // 保存所有品牌
    val preferenceListLD = MutableLiveData<List<PreferenceBrand>>()
    val similarBrandListLD: MutableResult<Pair<Int, List<PreferenceBrand>>> =
        MutableResult()
    val goToChooseCompletePageLD = MutableResult<List<PreferenceBrand>>()

    /**
     * 获取指定品牌相似品牌
     * @param layoutPosition:用户选中品牌在Adapter中的位置
     */
    fun getSimilarPreferenceBrandList(brandId: Int) {
        viewModelScope.launch {
            val data = SkGqlClient.skQuery(
                ListSimilarPreferenceBrandConfigQuery(id = brandId),
            )?.data?.listSimilarPreferenceBrandConfig?.filter { it != null }?.map { PreferenceBrand(it!!) }

            if (!data.isNullOrEmpty()) {
                similarBrandListLD.postValue(
                    Pair(brandId, data) // 返回品牌ID，及其关联品牌
                )
            }
        }
    }

    /**
     * 将用户喜欢的品牌提交服务器
     */
    fun reportPreferenceBrandConfig(preferences: List<PreferenceBrand>) {
        viewModelScope.launch {
            val selectedBrandNames = preferences
                .filter { !it.name.isNullOrEmpty() }
                .map { it.name!! }
                .toList()

            val response = SkGqlClient.skMutation(
                ReportPreferenceConfigMutation(
                    preferenceConfig = PreferenceConfigRequest(
                        selectedBrandList = Optional.present(selectedBrandNames)
                    )
                )
            )
            if (response?.data != null && response.data!!.reportPreferenceConfig == true) {
                goToChooseCompletePageLD.value = preferences
            } else {
                toast(com.starblink.basic.style.R.string.operate_fail)
            }
        }
    }

    fun loadTestPreferenceList() {
        viewModelScope.launch {
            // 品牌测试数据
            val test = """
                {"chooseOrNot":false,"type":1,"preferenceList":[{"id":97,"name":"NIKE","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/3db209b3-6581-41ce-96cc-ff271fce326b.png","userInterestIdList":null},{"id":101,"name":"THE NORTH FACE","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/ae8a141f-4403-4e6e-8425-c410322214c3.png","userInterestIdList":null},{"id":105,"name":"TOMMY HILFIGER","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/5e392173-e0b9-46f8-aab2-46ec79d02bc9.png","userInterestIdList":null},{"id":109,"name":"ARKET","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/ed029a4c-8aae-4f17-a2d6-89db0bffa626.png","userInterestIdList":null},{"id":113,"name":"FJÄLLRÄVEN","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/4db2b2c2-02f3-4c13-a5bb-ea35e50560cd.png","userInterestIdList":null},{"id":117,"name":"VAGABOND","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/e8b36184-7876-445d-8065-070cef47fb0b.png","userInterestIdList":null},{"id":121,"name":"H&M","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/c2415b0e-7b65-473a-84ed-07b690c6733b.png","userInterestIdList":null},{"id":125,"name":"LINDEX","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/4713bae9-76e8-42cf-8197-0f9bcc4933e1.png","userInterestIdList":null},{"id":129,"name":"SNÖ OF SWEDEN","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/3ded44fe-2377-4b28-a156-e51db2776db7.png","userInterestIdList":null},{"id":133,"name":"ACNE STUDIOS","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/407ce662-d380-403f-94f2-3176b2243728.png","userInterestIdList":null},{"id":137,"name":"BALENCIAGA","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/b808f204-13d3-442e-913e-91ec5ad6b417.png","userInterestIdList":null},{"id":141,"name":"MARC JACOBS","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/b7dd52c9-256b-49ab-91c4-cf417d7b32de.png","userInterestIdList":null},{"id":145,"name":"IKEA","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/be745b54-7b90-49bf-8cca-559c67a50651.png","userInterestIdList":null},{"id":149,"name":"HAY","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/902a3477-b1da-492e-b869-5fdf178d7b1d.png","userInterestIdList":null},{"id":153,"name":"RÖRSTRAND","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/65d468d7-bfab-43a5-bee0-b97d25ed45db.png","userInterestIdList":null},{"id":157,"name":"ÅHLENS","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/46bee460-3c03-4772-92f3-8e6b328cb5bd.png","userInterestIdList":null},{"id":161,"name":"MARIMEKKO","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/2673459f-5c78-4df4-92b8-a3160ba1e081.png","userInterestIdList":null},{"id":165,"name":"WMF","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/26b917b2-ac85-4a86-94ce-52127f246aa0.png","userInterestIdList":null},{"id":169,"name":"BOBBI BROWN","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/bc7ded45-a6cb-462e-afd4-33fd10af6e92.png","userInterestIdList":null},{"id":173,"name":"FILORGA","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/845dc0bd-d40b-419c-b9ae-d4c4095ed9b3.png","userInterestIdList":null},{"id":177,"name":"LANCÔME","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/a0d860da-c9a0-4a1f-af05-7549da870304.png","userInterestIdList":null},{"id":181,"name":"NINTENDO","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/bbbe5ee2-f71d-4ff5-80c1-7a78dc06f5e2.png","userInterestIdList":null},{"id":185,"name":"SONY","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/d17cef31-e716-4579-8186-2a10cd92ea4a.png","userInterestIdList":null},{"id":189,"name":"GOPRO","image":"https://test-kproject.oss-cn-hangzhou.aliyuncs.com/png/7606a852-fca0-43b3-ba3c-a6b74d1978f3.png","userInterestIdList":null}]}
            """
            GsonUtils.fromJson(
                test,
                ListPreferenceConfigABTestQuery.ListPreferenceConfigABTest::class.java
            )?.preferenceList?.filter { it.id != null }
                ?.map { PreferenceBrand(it) }?.let {
                    totalPreferenceList.addAll(it)
                    preferenceListLD.postValue(totalPreferenceList)
                } ?: run {
                // 请求失败
            }

//            SkGqlClient.skQuery(
//                ListPreferenceConfigABTestQuery()
//            )?.data?.listPreferenceConfigABTest?.preferenceList?.filter { it.id != null }
//                ?.map { PreferenceBrand(it) }?.let {
//                    totalPreferenceList.addAll(it)
//                    preferenceListLD.postValue(totalPreferenceList)
//            } ?: run {
//                // 请求失败
//            }
        }
    }
}